package com.akk.action;

import com.akk.business.UserBusiness;
import com.akk.business.UserCardBusiness;
import com.akk.business.UserWalletBusiness;
import com.akk.config.annotation.InjectUser;
import com.akk.module.user.entity.User;
import com.akk.module.user.entity.UserCard;
import com.akk.module.user.model.request.*;
import com.akk.module.user.model.request.UserTransferRequest;
import com.akk.utils.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/app/user")
@Api(value = "app-user",description = "用户信息接口")
public class UserAction {
    @Autowired
    private UserBusiness userBusiness;

    @Autowired
    private UserCardBusiness userCardBusiness;
    @Autowired
    private UserWalletBusiness userWalletBusiness;


    @RequestMapping(value = "/getuserinfo",method = RequestMethod.GET)
    @ApiOperation(value="用户信息",notes="获取用户信息",httpMethod = "GET",response = RestResponse.class)
    public RestResponse getUserInfo(@InjectUser User user) {
        return RestResponse.build(userBusiness.getUserInfo(user));
    }

    @RequestMapping(value = "/updatephone",method = RequestMethod.GET)
    @ApiOperation(value="修改手机号",notes="修改手机号",httpMethod = "GET",response = boolean.class)
    public boolean updatePhone(@RequestParam String newPhone, @InjectUser User user,
                             HttpServletRequest request) {
        return userBusiness.updatePhone(user.getPhone(), newPhone,user.getAppId(), request);
    }

    @RequestMapping(value = "/updatephoto",method = RequestMethod.POST)
    @ApiOperation(value = "修改头像",notes = "修改头像",httpMethod = "POST",response = boolean.class)
    public boolean updatePhoto(@InjectUser User user, @ApiParam MultipartFile photo) {
        return userBusiness.updatePhoto(user,photo);
    }

    @PostMapping(value = "/updatetranspwd",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改交易密码",notes = "修改交易密码",httpMethod = "POST",response = boolean.class)
    public boolean updateTransPwd(@InjectUser User user, @RequestBody UpdateTransPwdRequest request) {
        return userBusiness.updateTransPwd(user,request);
    }

    @PostMapping(value = "/forgettranspwd",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "忘记交易密码",notes = "忘记交易密码",httpMethod = "POST",response = Boolean.class)
    public boolean forgetTransPwd(@InjectUser User user,
                            @RequestBody @Valid ForgetTransPwdRequest request,
                            HttpServletRequest servletRequest){
        return userBusiness.forgetTransPwd(user,request,servletRequest);
    }

    @PostMapping(value = "/authentication",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "实名认证",notes = "实名认证",httpMethod = "POST",response = Boolean.class)
    public boolean authentication(@InjectUser User user,
                            @RequestBody @Valid AuthenticationRequest request) {
        return userBusiness.authentication(user,request);
    }

    @PostMapping(value = "/autoauth",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "自动实名",notes = "自动实名",httpMethod = "POST",response = RestResponse.class)
    public RestResponse autoAuth(@InjectUser User user, @RequestBody @Valid AutoAuthRequest request){
        return userBusiness.autoAuth(user,request);
    }

    @GetMapping(value = "/myspreadusers")
    @ApiOperation(value = "我的三级内的推广用户", notes = "我的三级内的推广用户", httpMethod = "GET", response = RestResponse.class)
    public RestResponse mySpreadUsers(@InjectUser User user){
        return RestResponse.build(userBusiness.mySpreadUsers(user));
    }

    @PostMapping("/usercards")
    @ApiOperation(value = "我的银行卡列表",notes = "查询我的银行卡列表",httpMethod = "POST",response = List.class)
    public List<UserCard> userCards(@InjectUser User user){
        return userCardBusiness.userCards(user.getUserId());
    }

    @PostMapping(value = "/addcard",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value="添加银行卡",notes = "添加银行卡",httpMethod = "POST",response = Boolean.class)
    public boolean addCard(@RequestBody @Valid AddUserCardRequest request, @InjectUser User user) {
        return userCardBusiness.addCard(request,user);
    }

    @GetMapping(value = "/delusercard")
    @ApiOperation(value="删除银行卡",notes = "删除银行卡",httpMethod = "GET",response = Boolean.class)
    public boolean delCard(@RequestParam String cardId) {
        return userCardBusiness.delCard(cardId);
    }


    @RequestMapping(value = "/walletinfo",method= RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取商户的钱包信息",notes = "获取商户的钱包信息,不存在则新建",httpMethod = "POST",response = RestResponse.class)
    public RestResponse walletInfo(@InjectUser User user) {
        return RestResponse.build(userWalletBusiness.walletInfo(user));
    }

    @RequestMapping(value = "/transferbyphone",method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "转账",notes = "向平台内的商户通过手机转账",httpMethod = "POST",response = boolean.class)
    public boolean transferByPhone(@RequestBody @Valid UserTransferRequest request,
                                   @InjectUser User user) {
        return userWalletBusiness.transferByPhone(request,user);
    }

    @RequestMapping(value = "/drawcashorder",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "发起提现到卡",notes = "用户提现到卡",httpMethod = "POST",response = boolean.class)
    public boolean drawCashOrder(@RequestBody @Valid UserDrawCashRequest request, @InjectUser User user) {
        return userWalletBusiness.drawCashOrder(request,user);
    }



}
